package com.flyiu.data.consistency.util;

import java.lang.reflect.Type;

import org.springframework.util.StringUtils;

import com.google.gson.Gson;

public class ArgumentJsonUtil {
	private static final Gson gson = new Gson();
	private static final String SPLIT_CHAR = "X@X";
	
	public static String toJson( Object[] args){
		if (args != null) {
			String context = "";
			Gson gson = new Gson();
			for (Object arg : args) {
				context += gson.toJson(arg, arg.getClass()) + SPLIT_CHAR;
			}
			return context;
		}
		return "";
	}
	
	public static Object[] toArg( String context,Type[]  types){
		Object[] param = null;
		if (!StringUtils.isEmpty(context)) {
			String[] args = context.split(SPLIT_CHAR);
			param = new Object[args.length];
			for (int i = 0; i < args.length; i++) {
				String arg = args[i];
				param[i] = gson.fromJson(arg, types[i]);
			}
		}
		return param;
	}
}
